/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.dynproperty.support;

import org.aopalliance.intercept.MethodInvocation;

import com.jinhe.tss.component.dynproperty.support.hibernate.HibernateDynProperty;
import com.jinhe.tss.component.support.persistence.entityaop.MatchByDaoMethodNameInterceptor;
import com.jinhe.tss.core.persistence.IDao;
import com.jinhe.tss.core.persistence.IEntity;

/** 
 * <p> ManipulateDynEntityInterceptor.java </p> 
 * 处理动态实体的拦截器（Interceptor）。
 * 包括对动态实体进行增加、删除、获取、修改等操作, 在dao上进行拦截。
 * 
 * @author Jon.King 2008/04/14 10:18:51 $ 
 */
public class ManipulateDynEntityInterceptor extends MatchByDaoMethodNameInterceptor {
    
    private DynProperty dp = new HibernateDynProperty();

    @SuppressWarnings("unchecked")
    public Object invoke(MethodInvocation invocation) throws Throwable {
        dp.init((IDao<IEntity>) invocation.getThis());
        
        Object returnObj = invocation.proceed();
        
        if(returnObj instanceof AbstractDynEntity) {
            AbstractDynEntity entity = (AbstractDynEntity) returnObj;
            switch(judgeManipulateKind(invocation.getMethod().getName())){
            case SAVE:
            case UPDATE:
                return dp.saveDynProperti4Entity(entity);
            case GET:
                return dp.getDynProperti4Entity(entity);
            case DELETE:
                dp.removeDynProperti4Entity(entity);
                return returnObj;
            }
        }
        return returnObj;
    }
}

